<?php

namespace App\Admin\Controllers;

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\UserStatistic;
use App\GameStatistic;
use App\ChargeStatistic;
use App\Region;
use App\EveryGame;
use App\Order;
use App\GameArchive;
use App\User;

class StatisticController extends Controller
{
	protected $color = [
		'#f56954',
		'#00a65a',
		'#f39c12',
		'#00c0ef',
		'#3c8dbc',
		'#d2d6de',
		'#ea124c',
		'#272525',
		'#e07f17',
		'#d6e410',
		'#4ce40f',
		'#10d8ef',
		'#1034ef',
		'#9b10ef',
		'#9108e4',
		'#337ab7',
		'#f56954',
		'#00a65a',
		'#f39c12',
		'#00c0ef',
		'#3c8dbc',
		'#d2d6de',
		'#ea124c',
		'#272525',
		'#e07f17',
		'#d6e410',
		'#4ce40f',
		'#10d8ef',
		'#1034ef',
		'#9b10ef',
		'#9108e4',
		'#337ab7',
		'#f56954',
		'#00a65a',
		'#f39c12',
		'#00c0ef',
		'#3c8dbc',
		'#d2d6de',
		'#ea124c',
		'#272525',
		'#e07f17',
		'#d6e410',
		'#4ce40f',
		'#10d8ef',
		'#1034ef',
		'#9b10ef',
		'#9108e4',
		'#337ab7',
	];

	/**
	 * [userIndex 每日用户统计]
	 * @return [type] [description]
	 */
	public function userIndex(Request $request)
	{
		$total = 0;
		$data = json_encode([]);
		$period = "";
		$users = [];
		if ($request->input('time')) {
			$period = $request->input('time');
			$time_arr = explode('~', $request->input('time'));
			$begin = $time_arr[0];
			$end = $time_arr[1];
			$total = User::whereBetween('created_at', [$begin, $end])->where('account', 'like', 'wx_%')->count();
			$data = [
				['新用户', $total]
			];
			$data = json_encode($data);
			$users = User::whereBetween('created_at', [$begin, $end])->where('account', 'like', 'wx_%')->get();
		}

		return view('admin.statistic.user', compact('data', 'total', 'period', 'users'));
	}

	/**
	 * [filter 筛选]
	 * @return [type] [description]
	 */
	public function filter()
	{
		$province = Region::where(['region_type' => 1])->get();
		$city = Region::where(['region_type' => 2])->get();
		$citys = [];
		foreach ($city as $c) {
			if (array_key_exists($c->parent_id, $citys)) {
				array_push($citys[$c->parent_id], ['id' => $c->id, 'name' => $c->region_name]);
			} else {
				$citys[$c->parent_id] = [['id' => $c->id, 'name' => $c->region_name]];
			}
		}
		$citys = json_encode($citys);

		return view('admin.statistic.userFilter', compact('province', 'citys'));
	}

	/**
	 * [gameIndex description]
	 * @return [type] [description]
	 */
	public function gameIndex(Request $request)
	{
		$total = 0;
		$data = json_encode([]);
		$period = "";
		$games = [];
		if ($request->input('time')) {
			$period = $request->input('time') ? $request->input('time') : date('Y-m-d', time()) . '~' . date('Y-m-d', strtotime('+1 day'));
			$time_arr = explode('~', $period);
			$begin = strtotime($time_arr[0]);
			$end = strtotime($time_arr[1]);
			if ($request->input('type') == 1) {//全部
				$total = GameArchive::whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				// $total = $total ? count($total): 0;
				//扣点点
				$kdd = GameArchive::where('base_info', 'like', '%"type":"kdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				//推到胡
				$tdh = GameArchive::where('base_info', 'like', '%"type":"tdh"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				//洪洞王牌
				$htwp = GameArchive::where('base_info', 'like', '%"type":"htwp"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				//忻州扣点点
				$xzkdd = GameArchive::where('base_info', 'like', '%"type":"xzkdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				//晋中麻将
				$jz = GameArchive::where('base_info', 'like', '%"type":"jz"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($kdd > 0 || $tdh > 0 || $htwp > 0 || $xzkdd > 0 || $jz > 0) {
					$data = [
						['扣点点', $kdd],	
						['推到胡', $tdh],
						['洪洞王牌', $htwp],
						['忻州扣点点', $xzkdd],
						['晋中麻将', $jz]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			} else if ($request->input('type') == 2) {//扣点点
				$total = GameArchive::where('base_info', 'like', '%"type":"kdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				//扣点点
				$kdd = GameArchive::where('base_info', 'like', '%"type":"kdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($kdd > 0) {
					$data = [
						['扣点点', $kdd]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::where('base_info', 'like', '%"type":"kdd"%')->whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			} else if ($request->input('type') == 3) {//推倒胡
				$total = GameArchive::where('base_info', 'like', '%"type":"tdh"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				$tdh = GameArchive::where('base_info', 'like', '%"type":"tdh"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($tdh > 0) {
					$data = [
						['推倒胡', $tdh]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::where('base_info', 'like', '%"type":"tdh"%')->whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			} else if ($request->input('type') == 4) {//洪洞王牌
				$total = GameArchive::where('base_info', 'like', '%"type":"htwp"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				$htwp = GameArchive::where('base_info', 'like', '%"type":"htwp"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($htwp > 0) {
					$data = [
						['洪洞王牌', $htwp]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::where('base_info', 'like', '%"type":"htwp"%')->whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			} else if ($request->input('type') == 5) {//忻州扣点
				$total = GameArchive::where('base_info', 'like', '%"type":"xzkdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				$xzkdd = GameArchive::where('base_info', 'like', '%"type":"xzkdd"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($xzkdd > 0) {
					$data = [
						['忻州扣点', $xzkdd]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::where('base_info', 'like', '%"type":"xzkdd"%')->whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			} else if ($request->input('type') == 6) {//晋中麻将
				$total = GameArchive::where('base_info', 'like', '%"type":"jz"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				$jz = GameArchive::where('base_info', 'like', '%"type":"jz"%')->whereBetween('create_time', [$begin, $end])->groupBy('room_uuid')->get()->count();
				
				if ($jz > 0) {
					$data = [
						['晋中麻将', $jz]
					];
					$data = json_encode($data);
				}
				$games = GameArchive::where('base_info', 'like', '%"type":"jz"%')->whereBetween('create_time', [$begin, $end])->orderBy('create_time', 'DESC')->groupBy('room_uuid')->get();
			}
		}
		
		return view('admin.statistic.game', compact('data', 'total', 'period', 'games'));
	}

	/**
	 * [gameFilter description]
	 * @return [type] [description]
	 */
	public function gameFilter()
	{
		$province = Region::where(['region_type' => 1])->get();
		$city = Region::where(['region_type' => 2])->get();
		$citys = [];
		foreach ($city as $c) {
			if (array_key_exists($c->parent_id, $citys)) {
				array_push($citys[$c->parent_id], ['id' => $c->id, 'name' => $c->region_name]);
			} else {
				$citys[$c->parent_id] = [['id' => $c->id, 'name' => $c->region_name]];
			}
		}
		$citys = json_encode($citys);

		return view('admin.statistic.gameFilter', compact('province', 'citys'));
	}

	/**
	 * [gameDetail 游戏详情]
	 * @return [type] [description]
	 */
	public function gameDetail(Request $request)
	{
		$room_uuid = $request->input('uuid');
		$games = GameArchive::where(['room_uuid' => $room_uuid])->get();
		$game = GameArchive::where(['room_uuid' => $room_uuid])->first();
		$users = [];
		if ($game) {
			$game = json_decode($game, true);
			$history = json_decode($game['history'], true);
			foreach ($history['seats'] as $seats) {
				$user = User::where(['userid' => $seats['userid']])->first();
				if ($user) {
					array_push($users, ['name' => base64_decode($seats['name']), 'avatar' => $user->headimg]);
				}
			}
		}
		$u_count = count($users);
		return view('admin.statistic.gameDetail', compact('users', 'games', 'u_count'));
	}

	/**
	 * [chargeIndex description]
	 * @return [type] [description]
	 */
	public function chargeIndex(Request $request)
	{
		$total = 0;
		$data = json_encode([]);
		$period = "";
		$charge_data = [];
		if ($request->input('time')) {
			$period = $request->input('time') ? $request->input('time') : date('Y-m-d', time()) . '~' . date('Y-m-d', strtotime('+1 day'));
			$time_arr = explode('~', $period);
			$begin = $time_arr[0];
			$end = $time_arr[1];
			$orders = Order::where(['pay_status' => 1, 'type' => 1])->whereBetween('updated_at', [$begin, $end])->groupBy('order_sn')->get();
			if ($orders) {
				foreach ($orders as $val) {
					$total += $val->order_amount;
				}
			}
			if ($total > 0) {
				$data = [
					['银票', $total]
				];
				$data = json_encode($data);
			}
			$charge_data = Order::where(['pay_status' => 1, 'type' => 1])->whereBetween('updated_at', [$begin, $end])->groupBy('order_sn')->get();
		}

		return view('admin.statistic.charge', compact('data', 'total', 'period', 'charge_data'));
	}

	/**
	 * [chargeFilter description]
	 * @return [type] [description]
	 */
	public function chargeFilter()
	{
		$province = Region::where(['region_type' => 1])->get();
		$city = Region::where(['region_type' => 2])->get();
		$citys = [];
		foreach ($city as $c) {
			if (array_key_exists($c->parent_id, $citys)) {
				array_push($citys[$c->parent_id], ['id' => $c->id, 'name' => $c->region_name]);
			} else {
				$citys[$c->parent_id] = [['id' => $c->id, 'name' => $c->region_name]];
			}
		}
		$citys = json_encode($citys);
		return view('admin.statistic.chargeFilter', compact('province', 'citys'));
	}

}	